# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Peter Budai <peterbudai@hotmail.com>

_realname=libgcrypt
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.9.2
pkgrel=2
pkgdesc="General purpose cryptographic library based on the code from GnuPG (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gnupg.org"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libgpg-error")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libgpg-error")
options=('staticlibs' 'strip')
source=("https://gnupg.org/ftp/gcrypt/libgcrypt/${_realname}-${pkgver}.tar.bz2"{,.sig}
        'libgcrypt-use-correct-def-file.patch'
        'Smarter-fig2dev-detection.all.patch')
# https://gnupg.org/signature_key.html
validpgpkeys=('D8692123C4065DEA5E0F3AB5249B39D24F25E3B6'
              '031EC2536E580D8EA286A9F22071B08A33BD3F06'
              '5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'
              '6DAA6E64A76D2840571B4902528897B826403ADA')
sha256sums=('b2c10d091513b271e47177274607b1ffba3d95b188bbfa8797f948aec9053c5a'
            'SKIP'
            '55cf915badebb4b3cfa671eea725aebb77be791aa39a19caef090ded4f38d8eb'
            'd6855fc9b7a3a7fa440be1d9ae3477f8c08524d1aab1a8c7bb26d62b55382f72')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/libgcrypt-use-correct-def-file.patch
  patch -p1 -i "${srcdir}"/Smarter-fig2dev-detection.all.patch

  autoreconf -fiv
}

build() {
  local -a extra_config
  case "${CARCH}" in
    i?86|x86_64)
      ;;
    *)
      extra_config+=(--disable-asm)
      ;;
  esac
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    "${extra_config[@]}" \
    --with-gpg-error-prefix=${MINGW_PREFIX}

  make
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make check || true
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install -j1
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
